/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package presentation.proportional;

import control.CBacheca;
import control.CGestioneRubrica;
import customGuiControl.ImagePanel;
import customGuiControl.RoundedPanel2;
import customGuiControl.roundedPanel;
import entity.xml.EMessaggio;
import entity.xml.EUtente;
import foundation.FMessaggi;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.Border;

/**
 *
 * @author Ennio
 */
public class PGestioneRubrica implements MouseListener{
    
    private static PGestioneRubrica istance= null;
    private JPanel panel;
    private JPanel panelInside;
    private CGestioneRubrica cGestRubr= null;
    private CBacheca cBacheca= null;
    private EUtente clickedUser= null;
    private String precTooltipText="";
    
    private PGestioneRubrica(){
        this.panel=new JPanel(new relativeMarginLayout(5f,5f, 5f, 5f));
        this.panelInside=new JPanel(new FlowLayout(FlowLayout.LEFT));
        this.panel.add(this.panelInside);
        this.cGestRubr= CGestioneRubrica.getIstance();
        this.cBacheca= CBacheca.getIstance();
        this.setPanelGestioneRubricaByUser(CBacheca.getIstance().getCurrUser());
    }
    public static PGestioneRubrica getInstance(){
        if(istance==null) istance= new PGestioneRubrica();
        return istance;
    }
    public void setPanelGestioneRubricaByUser(EUtente eu){
        this.panel.setOpaque(false);
        this.panelInside.setOpaque(false);
        ArrayList<EUtente> rubricaUtente= new ArrayList<EUtente>();
        rubricaUtente= this.cGestRubr.getRubricaByUser(eu);
        for(int i=0; i<rubricaUtente.size(); i++){
            PanelRubricaContact userPanel= this.printUserInRubrica(rubricaUtente.get(i));
            userPanel.addMouseListener(this);
            this.panelInside.add(userPanel);
        }
    }
    public PanelRubricaContact printUserInRubrica(EUtente eUser){
        
        PanelRubricaContact panel= new PanelRubricaContact(eUser);

        return panel;
                
    }
    
    public JPanel getPanel(){
        return this.panel;
    }
    
    public RoundedPanel2 getMenuSotto(gui instance){
        
        /*roundedPanel rp= new roundedPanel(20, new Color(255,255,255),
                                                            new Color(229,229,229),
                                                            2f, Color.BLACK,
                                                            12114.286f,100f,Color.BLACK,
                                                            0.8f,"menuSotto");*/
        RoundedPanel2 rp=new RoundedPanel2();
        rp.setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        
        gbc.gridx=0;
        gbc.gridy=0;
        gbc.weightx=0.49;
        gbc.weighty=1.0;
        gbc.fill=GridBagConstraints.BOTH;
        Utility.addFindButton(gbc, rp, this, instance);
        
        
        gbc.gridx=1;
        gbc.gridy=0;
        gbc.weightx=0.01;
        gbc.weighty=1.0;
        gbc.fill=GridBagConstraints.BOTH;
        Utility.addPaddingButton(gbc, rp);
        
        gbc.gridx=2;
        gbc.gridy=0;
        gbc.weightx=0.49;
        gbc.weighty=1.0;
        gbc.fill=GridBagConstraints.BOTH;
        Utility.addAggiungiContattoButton(gbc, rp, this, instance);
        
        gbc.gridx=3;
        gbc.gridy=0;
        gbc.weightx=0.01;
        gbc.weighty=1.0;
        gbc.fill=GridBagConstraints.BOTH;
        Utility.addPaddingButton(gbc, rp);
        
        gbc.gridx=4;
        gbc.gridy=0;
        gbc.weightx=0.49;
        gbc.weighty=1.0;
        gbc.fill=GridBagConstraints.BOTH;
        Utility.addVisualizzaBachecaButton(gbc, rp, this, instance);
        
        gbc.gridx=5;
        gbc.gridy=0;
        gbc.weightx=0.01;
        gbc.weighty=1.0;
        gbc.fill=GridBagConstraints.BOTH;
        Utility.addPaddingButton(gbc, rp);
        
        gbc.gridx=6;
        gbc.gridy=0;
        gbc.weightx=0.49;
        gbc.weighty=1.0;
        gbc.fill=GridBagConstraints.BOTH;
        Utility.addDettagliButton(gbc, rp, this, instance);
        
        return rp;
        
    }

    public CBacheca getcBacheca() {
        return cBacheca;
    }

    public CGestioneRubrica getcGestRubr() {
        return cGestRubr;
    }

    public EUtente getClickedUser() {
        return clickedUser;
    }
    
    
    
    public void mouseClicked(MouseEvent e) {
        if(e.getSource().getClass().toString().contains("ImagePanel")){
            
            String pathImg= ((ImagePanel) e.getSource()).getPathImg();
            
            if(pathImg.contains("visualizza_bacheca")){
                
                if(this.clickedUser!=null) this.cGestRubr.openBachecaContact(this.clickedUser);
                else JOptionPane.showMessageDialog(null, "Seleziona un contatto.");
                
            }
            
        }
        else if(e.getSource().getClass().toString().contains("PanelRubricaContact")){
            
            PanelRubricaContact panelClickedUser= (PanelRubricaContact)e.getComponent();
            String nickClickedUser= panelClickedUser.getNicknameUser().getText();
            
            if(this.clickedUser==null){
                
                this.clickedUser= new EUtente();
                this.clickedUser= this.cGestRubr.getContactBacheca(nickClickedUser);
                panelClickedUser.signAsChecked();
                
            }
            else{
                
                if(nickClickedUser.equals(this.clickedUser.getNickname())){
                    
                    this.clickedUser= null;
                    panelClickedUser.signAsUnchecked();
                    
                }
                else{
                    JOptionPane.showMessageDialog(null, "Prima di selezionare un nuovo contatto devi deselezionare il precedente.");
                }
                
            }
            
        }
        
    }

    public void mousePressed(MouseEvent e) {
    }

    public void mouseReleased(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
        
        if(e.getSource().getClass().toString().contains("messaggioPanel")){
            this.precTooltipText= gui.getIstance().getInfoTextPane().getText();
            gui.getIstance().getInfoTextPane().setText("Clicca per selezionare il contatto");
        }
                
    }

    public void mouseExited(MouseEvent e) {
    
        if(e.getSource().getClass().toString().contains("messaggioPanel")){
            gui.getIstance().getInfoTextPane().setText(this.precTooltipText);
        }

    }
}
